﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using TracNghiem.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;
using System.Data;

namespace TracNghiem.Areas.Admin.Models
{
    public class Bai_VietModels
    {
        public string id { get; set; }
        //[Required(ErrorMessage = "Tên chủ đề không được để trống!")]
        public string dichvu_id { get; set; }
        //[Required(ErrorMessage = "Thời gian không được để trống!")]
        public string name { get; set; }
        public string anh { get; set; }
        public string tomtat { get; set; }



        // public string status { get; set; }

        public string tendichvu { get; set; }
        public SelectList service_id { get; set; }
        public List<Bai_VietModels> getbaihoc()
        {
            List<Bai_VietModels> baihoc = new List<Bai_VietModels>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT a.*,b.name tendichvu FROM bai_hoc a,dichvu b where a.dichvu_id=b.id ");
            
            while (odr.Read())
            {

                baihoc.Add(new Bai_VietModels
                {
                    id = odr["id"].ToString(),
                    dichvu_id = odr["dichvu_id"].ToString(),
                    name = odr["name"].ToString(),
                    tendichvu = odr["tendichvu"].ToString(),
                    anh = odr["anh"].ToString(),
                    tomtat = odr["tomtat"].ToString(),


                });
            }
            General.conn_global.Close();
            return baihoc;
        }
        public void addbaihoc(Bai_VietModels ts)
        {

            //"INSERT INTO bai_hoc select nvl((select max(id)+1 from bai_hoc),1),'test',10',logo.png',tóm tắt nha from dual"
            string sql = "INSERT INTO bai_hoc";
            sql += " select nvl((select max(id)+1 from bai_hoc),1),'" + ts.name + "'," + ts.dichvu_id + ",'" + ts.anh + "','" + ts.tomtat + "' from dual";
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }
        public void updateBaihoc(Bai_VietModels ts)
        {
       


            string sql = "UPDATE bai_hoc SET name='" + ts.name + "', dichvu_id=" + ts.dichvu_id + ", anh='" + ts.anh + "', tomtat='" + ts.tomtat + "' where id=" + ts.id;
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }
        public void deleteBaiHoc(int id)
        {
            string sql = "DELETE FROM bai_hoc WHERE id=" + id + "";
            General.Lib_ExecuteNonQuery(sql);
            General.conn_global.Close();
        }



        internal List<Bai_VietModels> getbaihoc_DanhMuc(int id)
        {
            List<Bai_VietModels> baihoc = new List<Bai_VietModels>();
            string str = "SELECT a.*,b.name tendichvu FROM bai_hoc a,dichvu b where a.dichvu_id=b.id and b.id=" + id ;
            OracleDataReader odr = General.Lib_GetDataReader(str);

            while (odr.Read())
            {

                baihoc.Add(new Bai_VietModels
                {
                    id = odr["id"].ToString(),
                    dichvu_id = odr["dichvu_id"].ToString(),
                    name = odr["name"].ToString(),
                    tendichvu = odr["tendichvu"].ToString(),
                    anh = odr["anh"].ToString(),
                    tomtat = odr["tomtat"].ToString(),

                });
            }
            General.conn_global.Close();
            return baihoc;
        }

        public List<Bai_VietModels> FindBai_Viet(string str)
        {
            List<Bai_VietModels> baihoc = new List<Bai_VietModels>();
            string sql = "SELECT a.*,b.name tendichvu FROM bai_hoc a,dichvu b where a.dichvu_id=b.id  and "+str;
            OracleDataReader odr = General.Lib_GetDataReader(sql);
            while (odr.Read())
            {

                baihoc.Add(new Bai_VietModels
                {
                    id = odr["id"].ToString(),
                    dichvu_id = odr["dichvu_id"].ToString(),
                    name = odr["name"].ToString(),
                    tendichvu = odr["tendichvu"].ToString(),
                    anh = odr["anh"].ToString(),
                    tomtat = odr["tomtat"].ToString(),


                });
            }
            General.conn_global.Close();
            return baihoc;
        }
    }
}

